package com.example.recycler.bsr;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/3/16.
 */

public class WdView extends View{
    Path mPath;
    Context mContext;
    Paint mPaint;
    // 单个波的长度
    int mWaveLength = 900;
    // 波的高度
    int mWaveHeight = 500;
    // 水平漂移
    int distanceX = 0;
    private float startX=-6666,startY;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (distanceX>=mWaveLength) {
                        distanceX=0;
                    }
                    distanceX+=15;
                    invalidate();
                    mHandler.sendEmptyMessageDelayed(1,15);
                    break;
            }
        }
    };
    public WdView(Context context) {
        super(context);
    }

    public WdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mPath=new Path();
        mContext=context;
        mPaint=new Paint();
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (startX==-6666) {
            startX=-mWaveLength;
            startY=getHeight()-mWaveHeight-100;
        }
        mPath.reset();
        mPath.moveTo(startX+distanceX,startY);
        for (int i = 0; i < 3; i++) {
            mPath.rQuadTo(mWaveLength/4,-100,mWaveLength/2,0);
            mPath.rQuadTo(mWaveLength/4,100,mWaveLength/2,0);
        }
        mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());
        mPath.close();
        canvas.drawPath(mPath,mPaint);
    }

    public void startAnima(){
        mHandler.sendEmptyMessage(1);
    }
}
